package com.mbc.rmt.manager;

import java.sql.SQLException;
import java.util.HashMap;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;

import com.mbc.common.CommonSingleton;
import com.mbc.common.dao.CommonDAOFactory;
import com.mbc.common.dao.LogDAO;
import com.mbc.common.entities.Log;
import com.mbc.common.exception.DataDuplicationException;
import com.mbc.common.manager.AbstractMgr;
import com.mbc.common.utils.KeyGenerator;
import com.mbc.rmt.dao.RemittanceDAOFactory;
import com.mbc.rmt.dao.TypeDAO;
import com.mbc.rmt.data.TypeDataList;
import com.mbc.rmt.entities.Type;
import com.mbc.rmt.entities.TypeRpt;


public class TypeMgr extends AbstractMgr{
	
	private TypeDAO typeDAO;
	private LogDAO logDAO;
	
	public TypeMgr(){
		
	}
	public TypeMgr(String sessionId){
		this.sessionId = sessionId;
	}

	public void create(Type entity) throws Exception {
		openConnection();
		try {
			typeDAO = RemittanceDAOFactory.getTypeDAO(dbManager.getConnection());
			entity.setType_id(KeyGenerator.generateKey(getConnection()));
			typeDAO.create(entity);
			
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			
			Log l_logentity = new Log();
			l_logentity.setTablename("Type");
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setKey(entity.getType_id());
			l_logentity.setUserid(entity.getUserid());
			l_logentity.setLogstatus(Log.__SAVE_DATA);
			logDAO.getData(l_logentity);
			
		} catch (SQLException e) {
			if(DataDuplicationException.UNIQUE_KEY_ERROR_CODE == e.getErrorCode())
				throw new DataDuplicationException(e.getMessage());
			else
				throw e;
		} finally {
			closeConnection();
		}
	}
	
	public void update(Type entity) throws Exception {
		openConnection();
		try {
			typeDAO = RemittanceDAOFactory.getTypeDAO(dbManager.getConnection());
			typeDAO.update(entity);
			
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			
			Log l_logentity = new Log();
			l_logentity.setTablename("Type");
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setKey(entity.getType_id());
			l_logentity.setUserid(entity.getUserid());
			l_logentity.setLogstatus(Log.__UPDATE_DATA);
			logDAO.getData(l_logentity);
			
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public void delete(long key, long userid) throws Exception {
		openConnection();
		try {
			typeDAO = RemittanceDAOFactory.getTypeDAO(dbManager.getConnection());
			
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			
			Log l_logentity = new Log();
			l_logentity.setTablename("Type");
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setKey(key);
			l_logentity.setUserid(userid);
			l_logentity.setLogstatus(Log.__DELETE_DATA);
			logDAO.getData(l_logentity);
			
			typeDAO.delete(key);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	public Type findByCode(String code,long branchId)throws Exception{
		openConnection();
		try {
			typeDAO = RemittanceDAOFactory.getTypeDAO(dbManager.getConnection());
			return typeDAO.findByCode(code,branchId);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	public TypeDataList findTypes(Type cri)throws Exception{
		openConnection();
		try {
			typeDAO = RemittanceDAOFactory.getTypeDAO(dbManager.getConnection());
			return typeDAO.findBanks(cri);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	public TypeDataList getTypeList(TypeRpt type)throws Exception{
		openConnection();
		try {
			typeDAO = RemittanceDAOFactory.getTypeDAO(dbManager.getConnection());
			return typeDAO.getTypeList(type);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	public JasperPrint prepareTypeReport(TypeRpt type) throws Exception {
		openConnection();
		JasperPrint l_jsPrint = null;
		try {
			typeDAO = RemittanceDAOFactory.getTypeDAO(getConnection());

			JRResultSetDataSource l_jrRS = new JRResultSetDataSource(typeDAO.prepareTypeReport(type));

			 JasperReport l_jsRpt =(JasperReport)JRLoader.loadObjectFromFile(CommonSingleton.ABSOLUTE_PATH + "reports/Type.jasper");
		
			 HashMap<String, Object> l_params = new HashMap<String, Object>();
				l_params.put("companyname", "mbc");
			l_jsPrint = JasperFillManager.fillReport(l_jsRpt, l_params, l_jrRS);

		} catch (JRException je) {
			throw je;
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}

		return l_jsPrint;

	}
}
